package com.zys.singleton.type5;


public class SingletonTest06 {

	public static void main(String[] args) {
		System.out.println("双重检查");
		Singleton instance = Singleton.getInstance();
		Singleton instance2 = Singleton.getInstance();
		System.out.println(instance == instance2); // true
		System.out.println("instance.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());

	}

}

// 懒汉式(线程安全，同步方法)
class Singleton {
	private static volatile Singleton instance;

	private Singleton() {}

	//不能解决线程同步问题，多线程可能会产生多个实例,应该在创建实例对象再判断一次是否创建实例对象

	public static Singleton getInstance() {
		if(instance == null) {
			synchronized (Singleton.class) {
				instance = new Singleton();
			}

		}
		return instance;
	}
}